package Frame;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import Dao.DaoSqlData;
import Rule.LimitedDocument;
import Rule.NameTag;
import Rule.PatternCompile;
import Table.MyTable;

public class ManagerInsertPanel extends JPanel{

	final JScrollPane scrollPane;
	private JButton Button_insert;
	private JTextField TextField_Name;
	private JTextField TextField_No;
	private JTextField TextField_Pri;
	
	public ManagerInsertPanel(){
		
		JLabel Label_Name = new JLabel(NameTag.common_Name);
		JLabel Label_No = new JLabel(NameTag.common_No);
		JLabel Label_Pri = new JLabel(NameTag.common_Pri);		
		
		LimitedDocument limitedDoc = new LimitedDocument(20);
		LimitedDocument limitedDoc_1 = new LimitedDocument(9);
		LimitedDocument limitedDoc_2 = new LimitedDocument(5);
		
		TextField_Name = new JTextField(limitedDoc,"",7);
		TextField_No = new JTextField(limitedDoc_1,"",7);
		TextField_Pri = new JTextField(limitedDoc_2,"",7);
		Button_insert = new JButton(NameTag.insert_1);
		Button_insert.addActionListener(new ButtonActionListener());
		
		JPanel panel_1 = new JPanel();
		panel_1.add(Label_Name);
		panel_1.add(TextField_Name);
		panel_1.add(Label_No);
		panel_1.add(TextField_No);
		panel_1.add(Label_Pri);
		panel_1.add(TextField_Pri);
		panel_1.add(Button_insert);
		
		scrollPane=new JScrollPane();
		scrollPane.setBorder(BorderFactory.createTitledBorder(NameTag.common_Tittle));
		scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		
		displayDataTable();
		
		this.setLayout(new BorderLayout());	
		this.add(panel_1, BorderLayout.NORTH);
		this.add(scrollPane, BorderLayout.CENTER);
		
	}
	
	/**
	 * 返回查询所有值的倒序表格
	 */
	protected void displayDataTable (){
			
        Vector rows = null;      
        try {
			ResultSet rs = DaoSqlData.getPrdInfoByDesc();
			rows = new Vector();
		    while(rs.next()){
		    	Vector currow = new Vector();
				currow.add(rs.getString(2));
				currow.add(rs.getInt(3));
				currow.add(rs.getFloat(4));
				rows.add(currow);
			}
		} catch (Exception e) {
			
			e.printStackTrace();
		}
        MyTable mytable = MyTable.getTable(rows,false,false);	

	 	scrollPane.setViewportView(mytable);
	}

	private class ButtonActionListener  implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			
			String sName = TextField_Name.getText().trim();
			String sNo = TextField_No.getText().trim();
			String sPri = TextField_Pri.getText().trim();
			boolean bIsUpdateSuccess = false;
			
			if(sName.equals("") || sNo.equals("") || sPri.equals("")){
				JOptionPane.showMessageDialog(scrollPane, "不能为空值");
				return;
			}		
			
			if(! PatternCompile.isIntger(TextField_No.getText().trim())){
				JOptionPane.showMessageDialog(null, "品号输入不符");
				TextField_No.requestFocus();
				TextField_No.selectAll();
				return;
			}			
			if(! PatternCompile.isDecimal(TextField_Pri.getText().trim())){
				JOptionPane.showMessageDialog(null, "金额输入不符");
				TextField_Pri.requestFocus();
				TextField_Pri.selectAll();
				return;
			}
			
			int iNo = Integer.valueOf(sNo);
			float fPri = Float.parseFloat(sPri);
			try {
				
				bIsUpdateSuccess = DaoSqlData.setPrdInfo_singel(sName, iNo, fPri);
			} catch (Exception e1) {
				
				e1.printStackTrace();
			}
			if(bIsUpdateSuccess){
				displayDataTable();
				TextField_Name.setText("");
				TextField_Name.requestFocus();//获得焦点
				TextField_No.setText("");
				TextField_Pri.setText("");				
			}
		}
		
	}


}
